﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Npgsql;

namespace IonosphereViewer
{
    public partial class LoginPage : System.Web.UI.Page
    {
        // PostgeSQL-style connection string
        public static string constring = String.Format("Server={0};Port={1};" +
               "User Id={2};Password={3};Database={4};",
               ConfigurationManager.AppSettings["host"],
               ConfigurationManager.AppSettings["port"],
               ConfigurationManager.AppSettings["user"],
               ConfigurationManager.AppSettings["ps"],
               ConfigurationManager.AppSettings["db"]);

        public NpgsqlConnection conn = new NpgsqlConnection(constring);
        //--------------------------------------------------------------------------
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Error += new EventHandler(_Default_Error);
            txtLogin.Focus();
        }
        //--------------------------------------------------------------------------
        void _Default_Error(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Write("Page Error!<br />");
            Response.Write(string.Format("Error: <b>{0}</b>", Server.GetLastError().Message));
            Server.ClearError();
        }
        //--------------------------------------------------------------------------
        protected void RadButton1_Click(object sender, EventArgs e)
        {

            try
            {
                conn.Open();
                try
                {
                    NpgsqlCommand dccard = new NpgsqlCommand("SELECT username, pass FROM usercontrol WHERE username = @username AND pass = @pass", conn);
                    NpgsqlParameter param1 = new NpgsqlParameter();
                    param1.ParameterName = "username";
                    param1.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Varchar;
                    param1.Value = txtLogin.Text.ToString().Trim();
                    NpgsqlParameter param2 = new NpgsqlParameter();
                    param2.ParameterName = "pass";
                    param2.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Varchar;
                    param2.Value = txtPassword.Text.ToString();
                    dccard.Parameters.Add(param1);
                    dccard.Parameters.Add(param2);
                    NpgsqlDataReader er = dccard.ExecuteReader();
                    try
                    {
                        if (er.HasRows)
                        {
                            er.Read();
                            Session["User"] = er["username"];
                            Session["Password"] = er["pass"];
                            //Response.Redirect("~/ClientInfo.aspx");/*/
                            Server.Transfer("~/SFiles.aspx");
                        }
                        else
                        {
                            CustomValidator.IsValid = false;
                        }
                    }
                    finally
                    {
                        er.Close();
                    }
                }

                finally
                {
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                CustomValidator.IsValid = false;
            }
            //--------------------------------------------------------------------------
        }

    }
}